package com.pig.openg1.graphics;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.util.AttributeSet;
import android.util.Log;

import com.pig.openg1.data.Triangle;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/***
 * 绘制三角形
 */
public class DrawView1  extends GLSurfaceView implements GLSurfaceView.Renderer{

    private final String TAG = DrawView1.class.getSimpleName();
    private Triangle mTriangle;


    public DrawView1(Context context) {
        super(context);
        //初始化：
        setEGLContextClientVersion(2); //设置版本
        setRenderer(this); //设置渲染器
        setRenderMode(RENDERMODE_CONTINUOUSLY); //设置渲染模式


    }

    public DrawView1(Context context, AttributeSet attrs) {
        super(context, attrs);
        setEGLContextClientVersion(2); //设置版本
        setRenderer(this); //设置渲染器
        setRenderMode(RENDERMODE_CONTINUOUSLY); //设置渲染模式

    }

    //第二部渲染部分


    /***
     *
     * @param gl 界面创建
     * @param config 界面创建
     */
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        GLES20.glClearColor(0,0,0,1);// 设置背景色
        mTriangle = new Triangle(DrawView1.this);

    }

    /***
     * 界面改变时用
     * @param gl
     * @param width
     * @param height
     */
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {

        //设置视口
        GLES20.glViewport(0,0,width,height); //屏幕原点坐标，事物的大小
        float ratio = (float)width/height;
        Matrix.frustumM(mTriangle.mProMatrix,0,
                -ratio,ratio,
                -1,1,
                1,10);

        Matrix.setLookAtM(mTriangle.mVMatrix,0,
                0,0,3,
                    0,0,0,
                0,1,0);

    }



    @Override
    public void onDrawFrame(GL10 gl) {
        //清除之前的缓存
        GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT| GLES20.GL_COLOR_BUFFER_BIT);

        mTriangle.drawSelf();
    }


}


